package cvi.com.reader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.adapter.HomePageListAdapter;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.refresh.CustomRefreshFooter;
import cvi.com.reader.tool.refresh.CustomRefreshHeader;
import cvi.com.reader.widgets.MultiStateView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/7/24.
 * 热门作品页面
 */

public class PopularWorksActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.mylistview)
    ListView mylistview;
    @BindView(R.id.stateView)
    MultiStateView stateView;
    @BindView(R.id.no_data)
    TextView noData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int hotDataId = -1;
    private HomePageListAdapter homePageListAdapter;
    //热门数据
    private List<HotBean.RowsBean> homeHotList = new ArrayList<HotBean.RowsBean>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_popular_works;
    }

    @Override
    protected void initView() {
        initSmartRefreshLayout();
        stateView.getBackground().setAlpha(153);
        back.setOnClickListener(this);
        tittle.setText("热门作品");
        hotDataId = getIntent().getIntExtra("hotDataId", -1);
    }

    @Override
    protected void initData() {
        //绑定生命周期
        NetWorkManager.getInstance().getNetWorkApiService().getHomePageHotData(hotDataId).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(PopularWorksActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        //获取热门数据
                        smartRefreshLayout.finishRefresh(true);
                        setHotData(response);
                        stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                    }
                });
    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    //初始化下拉刷新控件
    private void initSmartRefreshLayout() {
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setRefreshHeader(new CustomRefreshHeader(PopularWorksActivity.this));
        smartRefreshLayout.setRefreshFooter(new CustomRefreshFooter(PopularWorksActivity.this, "加载中…"));
        smartRefreshLayout.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        smartRefreshLayout.setEnableFooterFollowWhenLoadFinished(true);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //  presenter.onLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                homeHotList.clear();
                initData();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }

    private void setHotData(JSONObject response) {
        JSONArray array = response.getJSONArray("rows");
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = (JSONObject) array.get(i);     //将array中的数据进行逐条转换
            HotBean.RowsBean rowsBean = (HotBean.RowsBean) JSONObject.toJavaObject(object, HotBean.RowsBean.class);  //通过JSONObject.toBean()方法进行对象间的转换
            homeHotList.add(rowsBean);
        }

        if (0 == homeHotList.size()) {
            noData.setVisibility(View.VISIBLE);
            smartRefreshLayout.setEnableRefresh(false);
        } else {
            smartRefreshLayout.setEnableRefresh(true);
            homePageListAdapter = new HomePageListAdapter(PopularWorksActivity.this, homeHotList);
            mylistview.setAdapter(homePageListAdapter);
            homePageListAdapter.notifyDataSetChanged();
            noData.setVisibility(View.GONE);
        }
        mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HotBean.RowsBean rowsBean = (HotBean.RowsBean) adapterView.getAdapter().getItem(i);
                Intent intent = new Intent(PopularWorksActivity.this, WorksDetailsAcitivity2.class);
                startActivity(intent);
                Hawk.put("WorksDetails", rowsBean);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
